import { get } from '@/api/fetch';

const top10GrossingApps = val => ({ type: 'GROSSING_TEN_APPS', val });
const topGrossingApps = val => ({ type: 'GROSSING_APPS', val });
const topFreeApps = val => ({ type: 'TOP_FREE_APPS', val });
const topTenApps = val => ({ type: 'TOP_TEN_APPS', val });

// 获取推荐app列表
export const getTop10GrossingApps = () => async dispatch => {
  const res = await get(
    'https://itunes.apple.com/hk/rss/topgrossingapplications/limit=10/json'
  );
  if (res && res.feed && res.feed.entry) {
    dispatch(top10GrossingApps(res.feed.entry));
  }
};

export const getTopGrossingApps = () => async dispatch => {
  const res = await get(
    'https://itunes.apple.com/hk/rss/topgrossingapplications/limit=100/json'
  );
  if (res && res.feed && res.feed.entry) {
    dispatch(topGrossingApps(res.feed.entry));
  }
};

// 按需获取top100 app列表
export const getTopFreeApps = () => async dispatch => {
  const res = await get(
    `https://itunes.apple.com/hk/rss/topfreeapplications/limit=100/json`
  );
  if (res && res.feed && res.feed.entry) {
    dispatch(topFreeApps(res.feed.entry));
  }
};

// 按需获取top10 app列表
export const getTop10FreeApps = () => async dispatch => {
  const res = await get(
    `https://itunes.apple.com/hk/rss/topfreeapplications/limit=10/json`
  );
  if (res && res.feed && res.feed.entry) {
    dispatch(topTenApps(res.feed.entry));
  }
};
